#!/usr/bin/python3.8.9
# -*- coding: utf-8 -*-

# @Author  : Tina Yu
# @Time    : 2021-7-19 20:44
"""
读取文件
"""
# 读取整个文件
print('*****************读取整个文件***************')
file_path = 'D:/download/pi_digits.txt'
with open(file_path) as file_object:
    contents = file_object.read()
print(contents.rstrip())

# 逐行读取
print('\n*****************逐行读取文件***************')
filename = 'pi_digits.txt'
with open(filename) as file_object:
    for line in file_object:
        print(line.rstrip())

# 逐行读取
print('\n*****************读取后存在列表中，with代码块外面使用***************')
filename = 'pi_digits.txt'
with open(filename) as file_object:
    # 从文件中读取每一行，并将其存储在一个列表中。
    lines = file_object.readlines()
for line in lines:
    print(line.rstrip())

# 使用文件的内容
print('\n*****************使用文件的内容***************')
filename = 'pi_million_digits.txt'
pi_string = ''
with open(filename) as file_object:
    lines = file_object.readlines()

for line in lines:
    pi_string += line.strip()

print(f'{pi_string[:52]}')
print(len(pi_string))
# 将字符串转换成浮点数
pi_float = float(pi_string)
print(pi_float + 1)

birthday = '900616'
if birthday in pi_string:
    print("包含您的生日！")
else:
    print("前1000000位中没有您的生日！")

# 练习10-1
print('\n*****************练习10-1：Python学习笔记***************')
filename = 'learning_python.txt'
# 读取整个文件
with open(filename) as f:
    contents = f.read()
print(contents.rstrip())

# 遍历文件对象
with open(filename) as f:
    for line in f:
        print(line.rstrip())

# 将各行存储在一个列表中，然后在with代码块外面打印
with open(filename) as f:
    lines = f.readlines()
contents_string = ''
for line in lines:
    contents_string += line
print(contents_string)

# 练习10-2
print('\n*****************练习10-2：替换***************')
with open(filename) as f:
    lines = f.readlines()
c_language = ''
for line in lines:
    c_language += line.replace('python', 'Java')
print(c_language)
